import { useEffect, useState } from 'react'
import { message } from 'antd'
import { isNullData } from '@/utils'
import { getYears, getFillingByYear, goBack } from '../service/ward'
import { IMock, IWard, IQuarter } from '../type/ward'
import { IYearList, IQuarterList } from '@/pages/DataFilling/type'

export default (): IWard => {
  // 年份列表
  const [years, setYears] = useState<IYearList[]>([])
  // 选择的年份
  const [curYears, setCurYears] = useState<string>()
  // 季度列表
  const [quarter, setQuarter] = useState<IQuarter[]>([])

  // 获取填报内容
  const getFilling = async () => {
    const params = {
      dataReportYear: curYears || '2023',
    }
    const res = await getFillingByYear(params)
    if (isNullData(res) && res.data) {
      setQuarter(res.data)
    }
  }

  const init = async () => {
    const res = await getYears()
    if (isNullData(res) && res.data) {
      setYears(res.data)
      setCurYears(res.data[0].dataReportYear)
    }
  }

  // 确认删除回调
  const deleteHandle = async (record: IQuarter) => {
    const params = {
      id: record.id,
    }
    const res = await goBack(params)
    if (isNullData(res)) {
      message.success('退回成功')
      getFilling()
    }
  }

  useEffect(() => {
    init()
  }, [])

  useEffect(() => {
    if (curYears) getFilling()
  }, [curYears])

  return {
    years,
    curYears,
    setCurYears,
    quarter,
    deleteHandle,
  }
}
